package com.solution.admin.customer.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.customer.entity.CustomerTagEntity;
import com.solution.admin.customer.service.CustomerTagService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 客户标签配置web控制层
 * @author solin
 * @date 2017-09-16 23:49:38
 */
@RequestMapping("/customerTag")
@Controller
public class CustomerTagController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(CustomerTagController.class);

	@Autowired
	private CustomerTagService ebCustomerTagService;
	
	/**
	 * 分页查询客户标签配置列表
	 * @param ebCustomerTag 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-09-16 23:49:38
	 */
	@RequestMapping("/listCustomerTagPage")
	@ResponseBody
	public JSONGrid listEbCustomerTagPage(CustomerTagEntity ebCustomerTag, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			PageBean pageBean = ebCustomerTagService.listEbCustomerTagPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询客户标签配置失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-09-16 23:49:38
	 */
	@RequestMapping("/toAddCustomerTag")
	public String toAddEbCustomerTag(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "customer/customerTag_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-09-16 23:49:38
	 */
	@RequestMapping("/toUpdateCustomerTag")
	public String toUpdateEbCustomerTag(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			CustomerTagEntity ebCustomerTag = ebCustomerTagService.getEbCustomerTagById(id);
			request.setAttribute("ebCustomerTag", ebCustomerTag);
		} catch (Exception e) {
			handleException("跳转编辑客户标签配置页面错误", logger, e);
		}
		return "customer/customerTag_form";
	}


	/**
	 * 新增或保存客户标签配置
	 * @param request 请求对象
	 * @param ebCustomerTagEntity 客户标签配置实体
	 * @return
	 * @author solin
 	 * @date 2017-09-16 23:49:38
	 */
	@RequestMapping("/saveOrUpdateCustomerTag")
	@ResponseBody
	public Map<String, Object> saveEbCustomerTag(HttpServletRequest request, CustomerTagEntity ebCustomerTagEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		String msg = "";
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = ebCustomerTagService.updateEbCustomerTag(ebCustomerTagEntity);
				msg = "更新客户级别配置成功"; 
			}else{
				result = (int)ebCustomerTagService.saveEbCustomerTag(ebCustomerTagEntity);
				msg = "添加客户级别配置成功";
				if (result > 0) {
					result = 1;
				}
			}
			resultMap.put("code", result);
			resultMap.put("msg", msg);
		} catch (Exception e) {
			handleException("添加客户标签配置失败", logger, e);
		}
		return resultMap;
	}
	
	/**
	 * 删除
	 * @param request
	 * @param id
	 * @return
	 * @author: solin
	 * @date: 2017年9月19日 上午11:40:20
	 */
	@RequestMapping("/deleteCustomerTag")
	@ResponseBody
	public Map<String, Object> deleteCustomerTag(HttpServletRequest request, long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		String msg = "";
		try {
			result = ebCustomerTagService.deleteEbCustomerTagById(id);
			resultMap.put("code", result);
			resultMap.put("msg", "删除成功");
		} catch (Exception e) {
			resultMap.put("code", result);
			resultMap.put("msg", "删除失败");
			handleException("删除客户标签配置失败", logger, e);
		}
		return resultMap;
	}
	
}
